﻿using BuyWithoutLoseMoney.Core.Models;
using BuyWithoutLoseMoney.Core.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Linq;

namespace BuyWithoutLose.Controllers
{
    [AllowAnonymous]
    public class NewsController : Controller
    {
        private readonly ILogger<NewsController> _logger;
        private readonly IRepository<News, int> _newsRepostitory;
        private readonly IRepository<NewsType, int> _newsTypeRepostitory;

        public NewsController(ILogger<NewsController> logger,
            IRepository<News, int> newsRepostitory,
            IRepository<NewsType, int> newsTypeRepostitory
            )
        {
            _logger = logger;
            _newsRepostitory = newsRepostitory;
            _newsTypeRepostitory = newsTypeRepostitory;
        }
        /// <summary>
        /// 显示所有的新闻内容
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            var news =  _newsRepostitory.GetAllList();
            ViewBag.News = news;
            var newsTypes = _newsTypeRepostitory.GetAllList();
            return View(newsTypes);
        }
        /// <summary>
        /// 新闻详情
        /// </summary>
        /// <param name="id">当前新闻的ID</param>
        /// <returns></returns>
        public IActionResult NewsDetail(int id)
        {
            //var newsTypes = _newsTypeRepostitory.GetAllList();
            //ViewBag.NewsTypes = newsTypes;
            var news = _newsRepostitory.GetAllList().Where(x => x.Id == id).FirstOrDefault();
            return View(news);
        }
    }
}
